//
//  HomeVC.m
//  EJLX
//
//  Created by adc on 15/11/9.
//  Copyright © 2015年 江苏一键联信息科技有限公司. All rights reserved.
//

#import "HomeVC.h"
#import "PersonVC.h"//个人中心
#import "PersonBeforeLoginVC.h"
#import "PersonLoginVC.h"
#import "ServerForPublic.h"//便民服务
#import "ShoppingVC.h"///购物中心
#import "MyShopVC.h"//我的微店
#import "ErWeiMaViewController.h"
#import "ShouYinDingDanViewController.h"
#import "WeiDianDingDanViewController.h"
#import "WoDeWeiDianViewController.h"
#import "WoDeShouRuViewController.h"
#import "PersonLoginVC.h"
#import "HttpsManager.h"
#import "LoginVC.h"
#import "SearchViewController.h"
#import "ClassOfGoodsVC.h"
#import "ShoppingOfCartVC.h"
@interface HomeVC ()

@end

@implementation HomeVC


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
     
    JGProgressHUD *HUD = [[JGProgressHUD alloc] initWithStyle:JGProgressHUDStyleDark];
    HUD.textLabel.text = @"正在加载中";
    
    HUD.useProgressIndicatorView = YES;
 
    [HUD showInView:self.navigationController.view];

    [HttpsManager lunboID:0 class:0 Block:^(NSMutableArray *imgArr, NSMutableArray *Class) {
        self.lunboArray =  imgArr;
        [self addScrollView];
        
        [HUD dismiss];
    }];

    
    
    [HttpsManager requestZiDongDengLuCompletion:^(int errcode, NSString *errmsg)
     {
         [[NSNotificationCenter defaultCenter] postNotification:[NSNotification notificationWithName:[NSString stringWithFormat:@"%d",errcode] object:nil]];
         if (errcode==0)
         {
             

         }
         else
         {
             LoginVC*vc=[[LoginVC alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
         }
         
         
     }];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notice:) name:@"10010" object:nil];

    self.view.backgroundColor = [UIColor whiteColor];
    
    _timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(timerBtnClick:) userInfo:nil repeats:YES];
    [self creatNav];
    [self creatTabbarView];
    
    [self addBtn];
       [GMDCircleLoader hideFromView:self.view animated:YES];
}
-(void)notice:(id)sender{
    NSLog(@"notice %@",sender);
}
-(void)timerBtnClick:(UIButton *)sender{
    static int i=0;
    i++;
    //轮播
    
}
-(void)creatNav{
   UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDE, 44)];
    navView.backgroundColor = RGB_COLOR(0x00c49f);
    [self.view addSubview:navView];
    
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    leftBtn.frame = CGRectMake(10, 7, 30, 30);
    leftBtn.layer.cornerRadius = 20;
    [leftBtn setImage:[UIImage imageNamed:@"个人中心"] forState:UIControlStateNormal] ;
    [leftBtn addTarget:self action:@selector(goToPersonBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:leftBtn];
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.frame = CGRectMake(WIDE-50, 7, 30, 30);
    rightBtn.layer.cornerRadius = 20;
    rightBtn.tag = 102;
    [rightBtn setImage:[UIImage imageNamed:@"搜索"] forState:UIControlStateNormal] ;
    [rightBtn addTarget:self action:@selector(searchBtn) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:rightBtn];
    
    UIImageView *img = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"logo"]];
    img.frame = CGRectMake(WIDE/2-50, 7, 100, 30);
    [navView addSubview:img];
    
   }
-(void)goToPersonBtn{
    [HttpsManager requestZiDongDengLuCompletion:^(int errcode, NSString *errmsg)
     {
         
         if (errcode==0)
         {
             if ([HttpsManager isLogin]) {
                 PersonLoginVC *vc = [[PersonLoginVC alloc] init];
                 
                 [self.navigationController pushViewController:vc animated:YES];
             }else{
                 PersonBeforeLoginVC *before = [[PersonBeforeLoginVC alloc] init];
                 before.navigationController.navigationBarHidden = YES;
                 [self.navigationController pushViewController:before animated:YES];
             }

         }
         else
         {
             LoginVC*vc=[[LoginVC alloc]init];
             [self.navigationController pushViewController:vc animated:YES];
         }
        
     }];
    

}
-(void)searchBtn{
   
    SearchViewController *search = [[SearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];


}
-(void)creatTabbarView{
    //tabbar自定义还是系统的
    UIView *tabbarView = [[UIView alloc] initWithFrame:CGRectMake(0, HEIGHT-56,WIDE, 56)];
    tabbarView.backgroundColor = RGB(240, 240, 240);
    [self.view addSubview:tabbarView];
    NSArray *tabTitleArr = @[@"操作手册",@"我的二维码",@"我的订单"];
    NSArray *tabImageArr = @[@"操作手册首页",@"我的二维码首页",@"我的订单首页"];
   // NSArray *imageArrH = @[@"操作手册",@"我的二维码",@"我的订单"];
    for (int i =0 ; i<3; i++) {
        UIButton *tabbarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        tabbarBtn.frame = CGRectMake((WIDE-25*3)/6.f+(25+(WIDE-25*3)/3.f)*i, 5, 25, 30);
        // tabbarBtn.layer.cornerRadius = 20;
        tabbarBtn.tag = 103+i;
       
        [tabbarBtn setImage:[UIImage imageNamed:tabImageArr[i]] forState:UIControlStateNormal] ;
       // [tabbarBtn setImage:[UIImage imageNamed:imageArrH[i]] forState:UIControlStateHighlighted];
        [tabbarBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UILabel *lb = [[UILabel alloc] initWithFrame:CGRectMake(((float)WIDE/3)*i+25, 35,((float)WIDE/3)-50, 20)];
        lb.text = tabTitleArr[i];
        lb.font = [UIFont preferredFontForTextStyle:UIFontTextStyleCallout];
        lb.font = [UIFont systemFontOfSize:10.0f] ;
    lb.textAlignment = NSTextAlignmentCenter;
        [tabbarView addSubview:lb];
        [tabbarView addSubview:tabbarBtn];
    }

}
-(void)addScrollView{
  //  NSMutableArray *arr = [NSMutableArray arrayWithArray:self.lunboArray];
  //@[@"http://121.42.211.76/images/curouselad/tjyj.jpg",@"http://121.42.211.76/images/curouselad/jd0fl.jpg"];
    
    _scroll = [[UIScrollView alloc]initWithFrame: CGRectMake(0, 64, WIDE, 210)];
    _scroll.showsHorizontalScrollIndicator = NO;
    _scroll.contentSize = CGSizeMake(WIDE*self.lunboArray.count, 180);
    _scroll.pagingEnabled = YES;
    _scroll.delegate = self;
    _scroll.backgroundColor = [ UIColor whiteColor];
    [self.view addSubview:_scroll];

    
        for (int i=0; i<self.lunboArray.count; i++)
        {
            
         //   [GMDCircleLoader hideFromView:self.view animated:YES];
            
            UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(0+i*WIDE, 0, WIDE, 210)];
            img.tag = 100+i;
            
            [img sd_setImageWithURL:self.lunboArray[i] placeholderImage:[UIImage imageNamed:@"默认图片.jpg"]];
            img.userInteractionEnabled = YES;
            
            UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(headerImgSeletor:)];
            [img addGestureRecognizer:tap];
            [_scroll addSubview:img];
            
        }
            _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(WIDE/2.f, 240, WIDE/2.f, 20)];
        _pageControl.numberOfPages =self.lunboArray.count;
        _pageControl.currentPage = 0;
        _pageControl.pageIndicatorTintColor=[UIColor whiteColor];
        _pageControl.currentPageIndicatorTintColor = [UIColor blueColor];
        [self.view addSubview:_pageControl];

}
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    _pageControl.currentPage = scrollView.contentOffset.x/ WIDE;
}
-(void)headerImgSeletor:(UIGestureRecognizer*)temp
{
    GoToWebView *web = [[GoToWebView alloc] initWithurl:[NSURL URLWithString:[NSString stringWithFormat:@"http://120.24.92.147/img_phone/%ld.html",temp.view.tag-99]] title:@"说明"];
    [self.navigationController pushViewController:web animated:YES];
}

-(void)addBtn{
   self.imageArray = @[@"购物中心.jpg",@"我的微店.jpg",@"便民服务.jpg",@"金融超市.jpg"];
   
    for (int i=0; i<4; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(i%2*((WIDE-15)/2.f+5)+5, i/2*((HEIGHT-280-56-15)/2.f+5)+280,(WIDE-15)/2.f , (HEIGHT-280-56-15)/2.f );
        switch (i) {
            case 0:
                 btn.backgroundColor = RGB_COLOR(0x589dfa);
                break;
            case 1:
                btn.backgroundColor = RGB_COLOR(0x00c49f);
                break;

            case 2:
                btn.backgroundColor = RGB_COLOR(0xef6d6d);
                break;

            case 3:
                btn.backgroundColor = RGB_COLOR(0x9e73da);
                break;

            default:
                break;
        }
        
        btn.tag = 106+i;
        [btn setImage:[UIImage imageNamed:self.imageArray[i]] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        
    }
    UIButton *moneyBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    
    int width = (WIDE == 320) ?100: 120;
    moneyBtn.frame = CGRectMake(WIDE/2.f-width/2,(HEIGHT-280-56-20)/2.f+280-width/2, width, width) ;
    moneyBtn.backgroundColor = [UIColor whiteColor];
    moneyBtn.tag = 110;
    moneyBtn.layer.cornerRadius =width/2;
    [moneyBtn setImage:[UIImage imageNamed:@"我的收入"] forState:UIControlStateNormal];
    [moneyBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:moneyBtn];}

-(void)btnClick:(UIButton *)sender{
    //106--110按钮图标
//    if (sender.tag == 101) {
//        if ([HttpsManager isLogin] == YES) {
//            PersonLoginVC *userVC = [[PersonLoginVC alloc] init];
//            [self.navigationController pushViewController:userVC animated:YES];
//        }
//        else{
//            PersonBeforeLoginVC *people = [[PersonBeforeLoginVC alloc] init];
//            [self.navigationController pushViewController:people animated:YES];
//        }
//        
//        
//        
//        
//    }else if (sender.tag==102){
//        //搜索
//            }

if (sender.tag==103){
        GoToWebView *web = [[GoToWebView alloc] initWithurl:[NSURL URLWithString:[NSString stringWithFormat:@"%@/comm/operation_manual",HTTPS_ID]] title:@"操作手册"];
        [self.navigationController pushViewController:web animated:YES];
        
    }else if (sender.tag==104){
        
        ErWeiMaViewController*vc=[[ErWeiMaViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
        
    }else if (sender.tag==105)
    {
        if ([HttpsManager isLogin])
        {
            ShouYinDingDanViewController*showYin=[[ShouYinDingDanViewController alloc]init];
            showYin.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"020收银订单" image:[UIImage imageNamed:@"O2O灰"] selectedImage:[UIImage imageNamed:@"O2O"]];
            UINavigationController*shouYinVc=[[UINavigationController alloc]initWithRootViewController:showYin];
            WeiDianDingDanViewController*weiDian=[[WeiDianDingDanViewController alloc]init];
            weiDian.tabBarItem=[[UITabBarItem alloc]initWithTitle:@"微店订单" image:[UIImage imageNamed:@"订单灰"] selectedImage:[UIImage imageNamed:@"订单"]];
            UINavigationController*weiDianVc=[[UINavigationController alloc]initWithRootViewController:weiDian];
            UITabBarController*tabbar=[[UITabBarController alloc]init];
            tabbar.viewControllers=@[shouYinVc,weiDianVc];
            UIWindow*window=[UIApplication sharedApplication].delegate.window;
            window.rootViewController=tabbar;
            
            return;
        }
        else{
//            UIAlertView*alt=[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请先登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去登录", nil];
//            alt.tag=2;
//            [alt show];
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        //[UIView transitionWithView:window duration:2 options:UIViewAnimationOptionTransitionCurlUp animations:nil completion:nil];
        
    }else if (sender.tag==106){
        //购物中心 NSArray *tabTitleArr = @[@"首页",@"分类",@"购物车"];
//        NSArray *tabImageArr = @[@"首页灰",@"分类灰",@"购物车"];
//        NSArray *imgh = @[@"首页绿",@"分类绿",@"购物车"];
        ShoppingVC *shopping = [[ShoppingVC alloc] init];
        [self.navigationController pushViewController:shopping animated:YES];
//        shopping.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"首页" image:[UIImage imageNamed:@"首页灰"] selectedImage:[UIImage imageNamed:@"首页绿"]];
//       UINavigationController *shop = [[UINavigationController alloc] initWithRootViewController:shopping];
//        
//        ClassOfGoodsVC *class = [[ClassOfGoodsVC alloc] init];
//        class.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"分类" image:[UIImage imageNamed:@"分类灰"] selectedImage:[UIImage imageNamed:@"分类绿"]];
//        UINavigationController *classGoods = [[UINavigationController alloc] initWithRootViewController:class];
//        
//        ShoppingOfCartVC *car = [[ShoppingOfCartVC alloc] init];
//        car.tabBarItem = [[UITabBarItem alloc] initWithTitle:@"购物车" image:[UIImage imageNamed:@"购物车"] selectedImage:[UIImage imageNamed:@"购物车"]];
//        UINavigationController *cart = [[UINavigationController alloc] initWithRootViewController:car];
//        
//        UITabBarController*tab=[[UITabBarController alloc]init];
//        tab.viewControllers=@[shop,classGoods,cart];
//        tab.tabBar.tintColor = RGB_COLOR(0x00c49f);
//        tab.tabBarItem.imageInsets = UIEdgeInsetsMake(6, 0, -10,0);
//        UIWindow*window=[UIApplication sharedApplication].delegate.window;
//        window.rootViewController=tab;
    }else if (sender.tag==107)
    {
        //我的微电
        
        
        if ([HttpsManager isLogin])
        {
            WoDeWeiDianViewController*vc=[[WoDeWeiDianViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            return;
        }
        else{
//            UIAlertView*alt=[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请先登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去登录", nil];
//            alt.tag=1;
//            [alt show];
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            
            //SHOW_ALERT(@"请先登录");
        }
        
        //        MyShopVC *myShop = [[MyShopVC alloc] init];
        //        [self.navigationController pushViewController:myShop animated:YES];
        
        
    }else if (sender.tag==108){
        //便民
        ServerForPublic *publicVC = [[ServerForPublic alloc] init];
        [self.navigationController pushViewController:publicVC animated:YES];
    }else if (sender.tag==109){
        //超市
        
    }else if (sender.tag==110)
    {
        //收入
        
        if ([HttpsManager isLogin])
        {
            WoDeShouRuViewController*vc=[[WoDeShouRuViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            return;
        }
        else{
//            UIAlertView*alt=[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"请先登录" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"去登录", nil];
//            alt.tag=3;
//            [alt show];
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            //SHOW_ALERT(@"请先登录");
        }
        
        
        
        
    }
}
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1)
    {
        if (buttonIndex==1)
        {
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
        
        
        
    }
    if (alertView.tag==2)
    {
        if (buttonIndex==1)
        {
            NSLog(@"555");
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    if (alertView.tag==3)
    {
        if (buttonIndex==1)
        {
            NSLog(@"555");
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    if (alertView.tag==4)
    {
        if (buttonIndex==1)
        {
            NSLog(@"555");
            LoginVC*vc=[[LoginVC alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
